package com.focusai.efairy.ui.activity.dev;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.request.PostDeviceUpdateRequest;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;

import java.util.ArrayList;

/**
 * 信息升级 信息编辑和保存页
 * Created by lifeng 2018/07/02
 */

public class DevUpgradeActivity extends SwipeBackBaseActivity implements View.OnClickListener {

    public static final int SELECT_ENTERPRISE_NAME_RESULT = 0x010;
    public static final int SELECT_PURPOSE_RESULT = 0x012;
    public static final int SELECT_VERSION_RESULT = 0x014;
    private TextView text_current_version, text_enterprise_name, text_purpose;
    private TextView  tv_select_enterprise_name,tv_select_purpose,tv_select_version;
    private String efairydevice_id,efairydevice_version_date,efairydevice_version;
    private View ll_select_enterprise_name, ll_select_purpose, ll_select_version;//选择企业名称，选择用途，选择版本
    private String enterprise_id,purpose_id,versionCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dev_upgrad);
        initView();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem1 = menu.add(MENU_GROUP_ID, MENU_ITEM_ADD, 0, R.string.save).setTitle(R.string.save);
        MenuItemCompat.setShowAsAction(menuItem1, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_ADD) {
            if(checkFrom(1)){
                save();
            }

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initView() {
        text_current_version = findView(R.id.text_current_version);
        text_enterprise_name = findView(R.id.text_enterprise_name);
        text_purpose = findView(R.id.text_purpose);
        ll_select_enterprise_name = findView(R.id.ll_select_enterprise_name);
        ll_select_purpose = findView(R.id.ll_select_purpose);
        ll_select_version = findView(R.id.ll_select_version);
        tv_select_enterprise_name = findView(R.id.tv_select_enterprise_name);
        tv_select_purpose = findView(R.id.tv_select_purpose);
        tv_select_version = findView(R.id.tv_select_version);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            text_current_version.setText(bundle.getString("version", ""));//软件版本
            text_enterprise_name.setText(bundle.getString("enterprise_name", ""));//企业名称
            text_purpose.setText(bundle.getString("purpos", ""));//用途
            efairydevice_id = bundle.getString(DevConfigureActivity.KEY_DEV_ID, "");
            efairydevice_version_date =  bundle.getString("efairydevice_version_date", "");
            efairydevice_version =  bundle.getString("efairydevice_version", "");
        }
        ll_select_enterprise_name.setOnClickListener(this);
        ll_select_purpose.setOnClickListener(this);
        ll_select_version.setOnClickListener(this);
    }

    @Override
    public void initTitte() {
        setCenterTitleName(R.string.dev_upgrade_info);
    }


    @Override
    public void onClick(View v) {
        Intent intent =new Intent(this,DevCompanyCodeActivity.class);
        switch (v.getId()) {
            case R.id.ll_select_enterprise_name:
                intent.putExtra("activity_type","companycode");
                startActivityForResult(intent,SELECT_ENTERPRISE_NAME_RESULT);
                break;
            case R.id.ll_select_purpose:
                intent.putExtra("activity_type","usagecod");
                startActivityForResult(intent,SELECT_PURPOSE_RESULT);
                break;
            case R.id.ll_select_version:

                if(checkFrom(0)){
                    intent.putExtra("activity_type","version");
                    intent.putExtra("enterprise_id",enterprise_id);
                    intent.putExtra("purpose_id",purpose_id);
                    startActivityForResult(intent,SELECT_VERSION_RESULT);
                }
                break;
            default:
                break;
        }

    }

    private boolean checkFrom(int level){
        if(TextUtils.isEmpty(enterprise_id)){
            showToast("请选择企业名称");
            return false;
        }
        if(TextUtils.isEmpty(purpose_id)){
            showToast("请选选择用途");
            return false;
        }
        if(level == 1){
            if(TextUtils.isEmpty(versionCode)){
                showToast("请选选择版本号");
                return false;
            }
        }

        return true;
    }


    /**
     * 提交设备升级
     */
    private void save() {
        PostDeviceUpdateRequest.DeviceEntry deviceEntry = new PostDeviceUpdateRequest.DeviceEntry();
        deviceEntry.efairydevice_id_list =  new ArrayList<>();
        deviceEntry.efairydevice_id_list.add(efairydevice_id);

         int length  = efairydevice_version_date.length();
         if(length  < 6){
             for(int i=length;i<6;i++){
                 efairydevice_version_date ="0"+efairydevice_version_date;
             }
         }else if(length > 6){
             efairydevice_version_date = efairydevice_version_date.substring(0,6);
         }

         int length2 = efairydevice_version.length();
        if(length2  < 2){
            for(int i=length2;i<2; i++){
                efairydevice_version = "0"+efairydevice_version;
            }
        }else if(length2 > 2){
            efairydevice_version = efairydevice_version_date.substring(0,2);
        }
        deviceEntry.dst_version = versionCode;
        deviceEntry.efairydevice_version = efairydevice_version_date+efairydevice_version;


        showSingleDialog("正在保存配置..");
        HttpManager.getInstance().sendRequest(new PostDeviceUpdateRequest(deviceEntry, new Response.Listener<String>() {
            @Override
            protected void onSuccess(String response) {
                closeSingleDialog();
                showToast("保存配置成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            protected void onFail(NetworkException exception) {
                closeSingleDialog();
                showToast("保存配置失败");
                doException(exception);
            }
        }));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null && resultCode == RESULT_OK){
            if(requestCode == SELECT_ENTERPRISE_NAME_RESULT){//企业信息
                if(data.hasExtra("code_id")){
                    enterprise_id = data.getStringExtra("code_id");
                    tv_select_enterprise_name.setText(data.getStringExtra("code_name"));
                }
            }else if(requestCode == SELECT_PURPOSE_RESULT){//用途
                purpose_id= data.getStringExtra("code_id");
                tv_select_purpose.setText(data.getStringExtra("code_name"));

            }else if(requestCode == SELECT_VERSION_RESULT){//版本
                versionCode = data.getStringExtra("code_name");
                tv_select_version.setText(versionCode);
            }
        }

    }
}


